package com.mediastory.spsserver.purchase.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mediastory.spsserver.content.domain.Content;
import com.mediastory.spsserver.content.service.ContentSvc;
import com.mediastory.spsserver.logging.service.LoggingSvc;
import com.mediastory.spsserver.purchase.service.PurchaseSvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping(value={"/purchase"})
public class PurchaseCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private PurchaseSvc purchaseSvc;
	
	@Autowired
	private LoggingSvc loggingSvc;
	
	@Autowired
	private ContentSvc contentSvc;
	
	/**
	 * 정산관리
	 * */
	@RequestMapping(value={"/purchaseData.do"})
	public Map<String, Object> getPurchaseData(HttpServletRequest request, HttpSession session) throws Exception {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> condition = new HashMap<String, Object>();

		//검색 parameter
		String osGubun = ServletRequestUtils.getStringParameter(request, "osGubun");
		String model = ServletRequestUtils.getStringParameter(request, "model");
		String startDate = ServletRequestUtils.getStringParameter(request, "startDate");
		String endDate = ServletRequestUtils.getStringParameter(request, "endDate");
		String contIdx = ServletRequestUtils.getStringParameter(request, "contIdx");
		String pageIndex = ServletRequestUtils.getStringParameter(request, "pageIndex");
		String check = "N";
		
		if(osGubun != null && osGubun.equals("all")) {
			osGubun = "";
			check = "Y";
		}
		if(model != null && model.equals("all")) {
			model = "";
		}

		condition.put("osGubun", osGubun);
		condition.put("model", model);
		condition.put("startDate", startDate);
		condition.put("endDate", endDate);
		condition.put("contIdx", contIdx);
		condition.put("pageIndex", pageIndex);
		

		if((osGubun != null && !osGubun.equals("")) || check.equals("Y")){
			resultMap.put("purchaseData", purchaseSvc.findbyListPurchaseData(condition));
		}
		
		String userRole = (String)session.getAttribute("userRole");
		//customerIdx		
		Integer cIdx = (Integer)session.getAttribute("cIdx");
		condition.put("pay", "Y");
		if(!userRole.equals("ROLE_ADMIN")) {
			condition.put("cIdx", cIdx);
		}
		//콘텐츠 목록
		List<Content> contentList = contentSvc.findbyList(condition);
		resultMap.put("contentList", contentList);
		
		//기종 목록(android)
		condition.put("osGubun", "android");
		resultMap.put("androidModelList", loggingSvc.findbyModelList(condition));
		
		//기종 목록(ios)
		condition.put("osGubun", "ios");
		resultMap.put("iosModelList", loggingSvc.findbyModelList(condition));
		
		return resultMap;
	}

}